package com.lv.common.ktx

import android.graphics.Typeface
import android.graphics.drawable.DrawableWrapper
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import com.google.android.material.tabs.TabLayout

/**
 * 作者： ygx
 * 创建日期：2023/5/15 9:00 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */

/**
 * 设置指示符固定宽度
 */
fun TabLayout.setSelectedTabIndicatorFixWidth(width: Float) {
    setSelectedTabIndicator(object : DrawableWrapper(tabSelectedIndicator) {
        override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
            var realLeft = left
            var realRight = right
            if ((right - left).toFloat() != width) {
                val center = left + (right - left).toFloat() / 2
                realLeft = (center - width / 2).toInt()
                realRight = (center + width / 2).toInt()
            }
            super.setBounds(realLeft, top, realRight, bottom)
        }
    })
}

/**
 * 设置tab选中的文字加粗
 */
fun TabLayout.setOnTabSelectedListener() = object : TabLayout.OnTabSelectedListener {
    override fun onTabSelected(tab: TabLayout.Tab?) {
        tab?.apply {
            val selecTab = text.toString().trim()
            val spannedString = selecTab.setTextSpan(Typeface.BOLD)
            tab.text = spannedString
        }
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
        tab?.apply {
            val unSelecTab = text.toString().trim()
            val spannedString = unSelecTab.setTextSpan()
            tab.text = spannedString
        }
    }

    override fun onTabReselected(tab: TabLayout.Tab?) {
    }


}